//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.zqj.project.common.request;

import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import com.zqj.project.common.database.mybatis.conditions.Wraps;
import jakarta.validation.constraints.NotNull;
import lombok.Data;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: PageParams
 * @Author: 22001010704 Zhang Qijian
 * @CreateTime: 2024/6/20 20:14
 * @Version: 1.0
 */
@Data
public class PageParams<T> {
    private @NotNull(message = "查询对象model不能为空") T model;
    private long size = 10L;
    private long current = 1L;
    private String sort = "id";
    private String order = "descending";
    private Map<String, Object> extra = HashMap.newHashMap(16);

    public PageParams(long current, long size) {
        this.size = size;
        this.current = current;
    }

    public PageParams() {
    }

    @JsonIgnore
    public <E> IPage<E> buildPage() {
        PageParams<T> params = this;
        return new Page<>(params.getCurrent(), params.getSize());
    }

    @JsonIgnore
    public <E> IPage<E> buildPage(Class<?> entityClazz) {
        PageParams<T> params = this;
        if (CharSequenceUtil.isEmpty(params.getSort())) {
            return new Page<>(params.getCurrent(), params.getSize());
        } else {
            Page<E> page = new Page<>(params.getCurrent(), params.getSize());
            List<OrderItem> orders = Lists.newArrayList();
            String[] sortArr = CharSequenceUtil.splitToArray(params.getSort(), ",");
            String[] orderArr = CharSequenceUtil.splitToArray(params.getOrder(), ",");
            int len = Math.min(sortArr.length, orderArr.length);
            for (int i = 0; i < len; ++i) {
                String humpSort = sortArr[i];
                String underlineSort = Wraps.getDbField(humpSort, entityClazz);
                orders.add(CharSequenceUtil.equalsAny(orderArr[i], "ascending", "ascend") ? OrderItem.asc(underlineSort) : OrderItem.desc(underlineSort));
            }
            page.setOrders(orders);
            return page;
        }
    }

    @JsonIgnore
    public long offset() {
        long current = this.current;
        return current <= 1L ? 0L : (current - 1L) * this.size;
    }

    @JsonIgnore
    public PageParams<T> put(String key, Object value) {
        if (this.extra == null) {
            this.extra = HashMap.newHashMap(16);
        }
        this.extra.put(key, value);
        return this;
    }

    @JsonIgnore
    public PageParams<T> putAll(Map<String, Object> extra) {
        if (this.extra == null) {
            this.extra = HashMap.newHashMap(16);
        }
        this.extra.putAll(extra);
        return this;
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof PageParams)) {
            return false;
        } else {
            PageParams<?> other = (PageParams) o;
            if (!other.canEqual(this)) {
                return false;
            } else if (this.getSize() != other.getSize()) {
                return false;
            } else if (this.getCurrent() != other.getCurrent()) {
                return false;
            } else {
                label64:
                {
                    Object this$model = this.getModel();
                    Object other$model = other.getModel();
                    if (this$model == null) {
                        if (other$model == null) {
                            break label64;
                        }
                    } else if (this$model.equals(other$model)) {
                        break label64;
                    }

                    return false;
                }
                label57:
                {
                    Object this$sort = this.getSort();
                    Object other$sort = other.getSort();
                    if (this$sort == null) {
                        if (other$sort == null) {
                            break label57;
                        }
                    } else if (this$sort.equals(other$sort)) {
                        break label57;
                    }
                    return false;
                }
                Object this$order = this.getOrder();
                Object other$order = other.getOrder();
                if (this$order == null) {
                    if (other$order != null) {
                        return false;
                    }
                } else if (!this$order.equals(other$order)) {
                    return false;
                }
                Object this$extra = this.getExtra();
                Object other$extra = other.getExtra();
                if (this$extra == null) {
                    return other$extra == null;
                } else {
                    return this$extra.equals(other$extra);
                }
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof PageParams;
    }

    @Override
    public int hashCode() {
        boolean PRIME = true;
        int result = 1;
        long $size = this.getSize();
        result = result * 59 + Long.hashCode($size);
        long $current = this.getCurrent();
        result = result * 59 + Long.hashCode($current);
        Object $model = this.getModel();
        result = result * 59 + ($model == null ? 43 : $model.hashCode());
        Object $sort = this.getSort();
        result = result * 59 + ($sort == null ? 43 : $sort.hashCode());
        Object $order = this.getOrder();
        result = result * 59 + ($order == null ? 43 : $order.hashCode());
        Object $extra = this.getExtra();
        result = result * 59 + ($extra == null ? 43 : $extra.hashCode());
        return result;
    }

    @Override
    public String toString() {
        return "PageParams(model=" + this.getModel() + ", size=" + this.getSize() + ", current=" + this.getCurrent() + ", sort=" + this.getSort() + ", order=" + this.getOrder() + ", extra=" + this.getExtra() + ")";
    }

}